package com.coursedesign.controller;

import com.coursedesign.bean.User;
import com.coursedesign.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//用户的控制类
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/userList")
    public List<User> getUserList(){

        List<User> userList = userService.getUserList();
        return userList;
    }

    @GetMapping("/userListXML")
    public List<User> getUserListXML(){

        List<User> userList = userService.getUserList();
        return userList;
    }


    @PostMapping("/userLogin")
    public Map userLogin(@RequestBody  User userLogin){
        User user = userService.userLogin(userLogin);
        Map<String ,Object> data = new HashMap<>();
        if (user!=null){
            data.put("code","00");
            data.put("msg","成功");
            user.setPassword("*******");
            data.put("userinfo",user);

        }else {
            data.put("code","88");
            data.put("msg","用户名或密码错误");
            data.put("userinfo",null);
        }
        return data;
    }

    //注册
    @PostMapping("/userRegister")
    public Map userRegister(@RequestBody User registerUser){
        int result = userService.userRegister(registerUser);
        Map<String ,Object> data = new HashMap<>();
        if (result!=0){
            data.put("code","00");
            data.put("msg","注册成功");
        }else {
            data.put("code","88");
            data.put("msg","注册失败");
        }

      return  data;
    }



}
